<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Nick extends CI_Controller 
{
	function __construct()
	{
		parent::__construct();
		
		//load helper
		$this->load->helper('login_helper');
			$this->load->helper('html');
		
			//load model

		$this->load->model('Mnick');
		


    //load libraries

    $this->load->library('pagination');

    $this->load->library('form_validation');
	// session
	 session_start();
		 if ( !isset($_SESSION['row_number']))

			$_SESSION['row_number']= 12;
	
	load_lang("admin");
	// check login
	check_login();
	}
		////************************ News  **************************/////
	function index($page='')
	{
		$limit = $_SESSION['row_number'];

		$config['base_url'] = site_url().'/admin/nick/index/';

		$config['first_link'] = 'First';

		$config['last_link'] = 'Last';

		$config['next_link'] = '&gt;&gt;';

		$config['prev_link'] = '&lt;&lt;';

		$config['cur_tag_open'] = '<a href="#" class="current">';

		$config['cur_tag_close'] = '</a>';

		$config['uri_segment'] = 4;

		$config['num_links'] = 2;

		$config['total_rows'] = $this->Mnick->count_all('tbl_nicks');

		$config['per_page'] = $limit;
		

		$this->pagination->initialize($config);

    if (empty($page))	$page = 0; //default value

    $data['result'] = $this->Mnick->list_data($page,$limit);// $this->Muser->user_review($page, $limit);

    $data['title'] = 'Hỗ trợ trực tuyến';

    $data['page'] = str_replace('&nbsp;', '', $this->pagination->create_links());

    $data['main'] = 'nick_review';

    $this->load->view('admin/layout/template_main', $data);
	}
	
	// NEws create
	
	function create()
	{
		 if ($this->input->post('create'))
		 

    {
	

      //set rules

   $this->form_validation->set_rules('nick_name', 'Tên tiêu đề', 'required');
  $this->form_validation->set_rules('nick_nicks', 'Nick Yahoo', 'required');
 //   $this->form_validation->set_rules('nick_skype', 'Nick Skype', 'required');

      // $this->form_validation->set_rules('img', 'img', 'required|callback_upload_file');
	
      if ($this->form_validation->run() && $this->nick_create_sb()  )

        redirect('admin/nick/', 'refresh');

      else
		{
			
        	$data['missing'] = $this->form_validation->_error_array;
			if(isset($_FILES['img']) && $_FILES['img']['error']==4)
				$data["missing"]=array("img"=>"Images field is required.");
			
		}

    }

    elseif ($this->input->post('close'))

		{

			redirect('admin/nick/', 'refresh');

		}



	
//	echo "<pre>";
	//print($data['cate']);
	//echo "</pre>";
		$data['main'] = 'nick_create';
	//$data['cate'] = $this->Mnick->get_cate();
	
    $data['title'] = 'Thểm tài khoản hỗ trợ ';

		$this->load->view('admin/layout/template_main', $data);
	}
		
			
				// function add nick
	function nick_create_sb()
	{
		if(isset($_POST["create"]))
		{
			
			$name = htmlentities($this->input->post("nick_name"),ENT_QUOTES,"UTF-8");
						$nicks = htmlentities($this->input->post("nick_nicks"),ENT_QUOTES,"UTF-8");
					$skype = htmlentities($this->input->post("nick_skype"),ENT_QUOTES,"UTF-8");
					$stauts = htmlentities($this->input->post("status"),ENT_QUOTES,"UTF-8");				
		$data = array(
			"nick_name" => $name,
			"nick_nicks" => $nicks,
			"nick_skype" => $skype,
			"nick_status" => $stauts,	
			"nick_modify" => time(),					
			
			);
			
	//
	
			
		if($this->Mnick->insert_data($data))
			return true;
		else
			return false;
		}
		else
			return false;
		
		
	}	
	
	#Update nick
		function update()
	{
	$id = $this->uri->segment(4);
		 if ($this->input->post('update'))
		 

    {
	

      //set rules

    $this->form_validation->set_rules('nick_name', 'Tên tiêu đề', 'required');
  $this->form_validation->set_rules('nick_nicks', 'Nick Yahoo', 'required');
   // $this->form_validation->set_rules('nick_skype', 'Nick Skype', 'required');

      // $this->form_validation->set_rules('img', 'img', 'required|callback_upload_file');
	
      if ($this->form_validation->run() && $this->nick_update_sb() )

        redirect('admin/nick/', 'refresh');

      else
		{
			
        	$data['missing'] = $this->form_validation->_error_array;
			
			
		}

    }

    elseif ($this->input->post('close'))

		{

			redirect('admin/nick/', 'refresh');

		}



	
//	echo "<pre>";
	//print($data['cate']);
	//echo "</pre>";
		$data['main'] = 'nick_update';
		$data["result"] =  $this->Mnick->info_from_id($id,'tbl_nicks');
	
//$data['cate'] = $this->Mnick->get_cate();
	
    $data['title'] =  $this->lang->line('title_nick_update');

		$this->load->view('admin/layout/template_main', $data);
	}
	
	# update proceessing
	function nick_update_sb()
	{
		
		if(isset($_POST["update"]))
		{
			$id = $this->uri->segment(4);
			
			
			$name = htmlentities($this->input->post("nick_name"),ENT_QUOTES,"UTF-8");
						$nicks = htmlentities($this->input->post("nick_nicks"),ENT_QUOTES,"UTF-8");
					$skype = htmlentities($this->input->post("nick_skype"),ENT_QUOTES,"UTF-8");
					$stauts = htmlentities($this->input->post("status"),ENT_QUOTES,"UTF-8");				
		$data = array(
			"nick_name" => $name,
			"nick_nicks" => $nicks,
			"nick_skype" => $skype,
			"nick_status" => $stauts,			
			"nick_modify" => time(),		
			);
			
	//
	
			
		if($this->Mnick->update_data($data,$id))
			return true;
		else
			return false;
		}
		else
			return false;
		
		
	
	
	
	
	
	
	
	
	
	
	}
	
 function upload_file()

  {

    //attachment file

    $input_file = 'img';
    if ($_FILES[$input_file]['error']==0  )

    {

      $path_directory = './upload/nick/';

      if ( ! is_dir($path_directory)) mkdir($path_directory);

      

      $config['upload_path'] = $path_directory;

      $config['allowed_types'] = 'gif|jpg|png';

      $config['max_size'] = '500'; //500kB



      $this->load->library('upload', $config);



      //upload file

      if ($this->upload->do_upload($input_file))

      {

        $info_file = $this->upload->data();

        $file_path = $info_file['file_name'];
		

      $_POST['img'] = $file_path;

      }

      else

      {
	  	
		//$data["error"]= $this->upload->display_errors();//$data["missing"] = array("img"=>$this->upload->display_errors());
        return false;

      }

    }

    return true;

  }

			
	
		////************************ News Category **************************/////
	
	
	
		// category  review	
	function cate()
	{
		$limit = $_SESSION['row_number'];

		$config['base_url'] = site_url().'/admin/nick/cate/';

		$config['first_link'] = 'First';

		$config['last_link'] = 'Last';

		$config['next_link'] = '&gt;&gt;';

		$config['prev_link'] = '&lt;&lt;';

		$config['cur_tag_open'] = '<a href="#" class="current">';

		$config['cur_tag_close'] = '</a>';

		$config['uri_segment'] = 4;

		$config['num_links'] = 2;

		$config['total_rows'] = $this->Mnick->count_all('nick_cate');

		$config['per_page'] = $limit;
		

		$this->pagination->initialize($config);

    if (empty($page))	$page = 0; //default value

    $data['result'] = $this->Mnick->list_cate($page,$limit);
	
	
	// $this->Muser->user_review($page, $limit);

    $data['title'] =  $this->lang->line('title_nick_cate_review');

    $data['page'] = str_replace('&nbsp;', '', $this->pagination->create_links());

    $data['main'] = 'nick_cate_review';

    $this->load->view('admin/template_main', $data);
	}
	
	
	// category create
	
	function cate_create()
	{
		 if ($this->input->post('create'))
		 

    {
	

      //set rules


      $this->form_validation->set_rules('title_en', 'Title English', 'required');

      $this->form_validation->set_rules('title_vi', 'Title Vietnamese', 'required');

  //   $this->form_validation->set_rules('title_cn', 'Title Chinese', 'required');

 //     $this->form_validation->set_rules('avatar', 'avatar', 'callback_upload_file');

      

      if ($this->form_validation->run() && $this->cate_create_sb())

        redirect('admin/nick/cate', 'refresh');

      else
		{
			
        	$data['missing'] = $this->form_validation->_error_array;
			
		}

    }

    elseif ($this->input->post('close'))

		{

			redirect('admin/nick/cate_review', 'refresh');

		}



	
		$data['main'] = 'nick_cate_create';

    $data['title'] =$this->lang->line('title_nick_cate_create');

		$this->load->view('admin/template_main', $data);
	}
		
	// function add category
	function cate_create_sb()
	{
		if(isset($_POST["create"]))
		{
		$name =array(
			"en" => $this->input->post("title_en"),
			"vi" => $this->input->post("title_vi"),
		//	"cn" => $this->input->post("title_cn")
			);
		$data = array(
			"nick_cate_name" => serialize($name),
			
			);
	
	
			
		if($this->Mnick->insert_cate($data))
			return true;
		else
			return false;
		}
		else
			return false;
		
		
	}	
	// update product category
	
	function cate_update()
	{
	$id= $this->uri->segment(4);
	if ($this->input->post('update'))

    {
	

      //set rules


      $this->form_validation->set_rules('title_en', 'Title English', 'required');

      $this->form_validation->set_rules('title_vi', 'Title Vietnamese', 'required');

//     $this->form_validation->set_rules('title_cn', 'Title Chinese', 'required');

 //     $this->form_validation->set_rules('avatar', 'avatar', 'callback_upload_file');

      

      if ($this->form_validation->run() && $this->cate_update_proccess())

        redirect('admin/nick/cate', 'refresh');

      else
		{
			
        	$data['missing'] = $this->form_validation->_error_array;
			
		}

    }

    elseif ($this->input->post('close'))

		{

			redirect('admin/nick/cate', 'refresh');

		}



	
		$data['main'] = 'nick_cate_update';
	$data['result']= $this->Mnick->info_from_id($id,'nick_cate');
    $data['title'] = $this->lang->line('title_nick_cate_update');

		$this->load->view('admin/template_main', $data);
	}
	
	// proccess data for update
	function cate_update_proccess()
	{
	$id= $this->uri->segment(4);
	if(isset($_POST["update"]))
		{
	$name =array(
			"en" => $this->input->post("title_en"),
			"vi" => $this->input->post("title_vi"),
	//		"cn" => $this->input->post("title_cn")
			);
		$data = array(
			"nick_cate_name" => serialize($name),
			
			);
	
			
		if($this->Mnick->update_cate($data,$id))
			return true;
		else
			return false;
		}
		else
			return false;
	}
	
		

	
	
	
	function cate_request()
	{//determine 'action'
		$require = array('show', 'delete');

		foreach ($_POST as $key=>$value)

		{
			if (in_array($key, $require))
			{
				$act = $key;
	
			}

		}
	//process 'action'

		if (! empty($act))

		{

			switch ($act)

			{

				case 'show': //show

				{
				
					$_SESSION['row_number'] = $this->input->post('row_number');
					
				} break;



				case 'delete': //delete

				{

					$this->Mnick->cate_delete();

				} break;

			}//end switch



			redirect('admin/nick/cate', 'refresh');

		}//end if

	}
	
		function request()
	{//determine 'action'
		$require = array('show', 'delete');

		foreach ($_POST as $key=>$value)

		{
			if (in_array($key, $require))
			{
				$act = $key;
	
			}

		}
	//process 'action'

		if (! empty($act))

		{

			switch ($act)

			{

				case 'show': //show

				{
				
					$_SESSION['row_number'] = $this->input->post('row_number');
					
				} break;



				case 'delete': //delete

				{

					$this->Mnick->delete();

				} break;

			}//end switch



			redirect('admin/nick/', 'refresh');

		}//end if

	}
		public function delete($id)
	{
		if($id >0 )
		
			$this->Mnick->del_item($id);
		redirect('admin/nick/', 'refresh');
		
	}
}









?>
